<?php
/**
 * @Project NUKEVIET 3.0
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2010 VINADES., JSC. All rights reserved
 * @Createdate 3/9/2010 23:25
 */

if ( ! defined( 'NV_IS_MOD_NEWS' ) ) die( 'Stop!!!' );
global $global_array_cat, $module_name, $module_info, $lang_module, $array_op, $module_data, $custom_title;
$subcat = array();
$cat = array();
$cat_alias = isset( $array_op[0] ) ? $array_op[0] : "";
if(isset($_GET['r'])){
    $top_level_alias = str_replace("/","",$_GET['r']);
}else{
    $top_level_alias = 0;
}

// all category level 1, for display.
$subcat[0] = array();

$current_cat_id = 0;
$current_alias = "";
if($top_level_alias){
    $current_alias = $top_level_alias;
}
else{
    $current_alias = $cat_alias;
}
foreach ( $global_array_cat as $key => $array_subcat ){
    // Current category
    if($array_subcat['alias'] === $current_alias){
        $current_cat_id = $array_subcat['catid'];
        $custom_title = $array_subcat['title'];
        $current_link = NV_BASE_SITEURL . "index.php?" .
                        NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" .
                        NV_NAME_VARIABLE . "=" . $module_name . "&amp;" .
                        NV_OP_VARIABLE . "=" . $current_alias;
        break;
    }
}
foreach ( $global_array_cat as $key => $array_subcat ){
     if($array_subcat['alias'] != $current_alias){
        $parentid = $array_subcat['parentid'];
        $catid = $array_subcat['catid'];

        // Xu ly voi category con
        if($parentid !=0){
            // La category con cua category hien tai
            if ($parentid == $current_cat_id){
                $link = NV_BASE_SITEURL . "index.php?" .
                        NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" .
                        NV_NAME_VARIABLE . "=" . $module_name . "&amp;" .
                        NV_OP_VARIABLE . "=" . $array_subcat['alias']. "&amp;" .
                        "f=n"; // come from mneu hoc.
                if($top_level_alias){
                    $link .= "&amp;" . "r=". $top_level_alias;
                }
                else{
                    $link .= "&amp;" . "r=". $cat_alias;
                }

                // Find all posts in this category.
                if($array_subcat['is_leaf']){
                    $order_by = "ORDER BY `publtime` DESC";
                    $sql = "SELECT `id`, `listcatid`, `title`, `alias` FROM `" . NV_PREFIXLANG . "_" . $module_data . "_" . $catid . "` WHERE `status`=1 AND (`publtime` < `exptime` OR `exptime`=0) AND `publtime` < " . NV_CURRENTTIME . " AND (`exptime`=0 OR `exptime`>" . NV_CURRENTTIME . ") " . $order_by;
                    $result = $db->sql_query( $sql );
                    while ( $item = $db->sql_fetchrow( $result ) ){
//                        $item['link'] = $global_array_cat[$catid]['link'] . "/" . $item['alias'] . "-" . $item['id'];
//                        if($top_level_alias){
//                             $item['link'] .= "&amp;" . "r=". $top_level_alias;
//                        }
//                        else{
//                             $item['link'] .= "&amp;" . "r=". $cat_alias;
//                        }
//                        $subcats[$catid][] = array(
//                            'catid' => $catid,
//                            'title' => $item['title'],
//                            'link' =>  $item['link']
//                        );
                    }
                }

                $sub = (isset($subcats[$catid]))?($subcats[$catid]):(array());
                $current = ($array_subcat['alias']===$cat_alias)?("current"):("");
                $cat[] = array(
                    'catid' => $catid,
                    'title' => $array_subcat['title'],
                    'link' => $link,
                    'current'=>$current,
                    'sub' => $sub,
                );
            }
        }
    }
}
//$sub = isset( $subcat[$current_cat_id] ) ? $subcat[$current_cat_id] : array();

$xtpl = new XTemplate( "block_catright.tpl", NV_ROOTDIR . "/themes/" . $module_info['template'] . "/modules/" . $module_file );
$xtpl->assign( 'LANG', $lang_module );
if ( ! empty( $cat ) )
{
    $my_head .= "<script type=\"text/javascript\"	src=\"" . NV_BASE_SITEURL . "themes/" . $module_info['template'] . "/js/jquery.category.news.js\"></script>\n";
    $my_head .= "<script type=\"text/javascript\">\n";
    $my_head .= "jQuery(document).ready(function(){\n";
    $my_head .= "$(\"#navmenu-v li\").hover(function(){\n";
    $my_head .= "	$(this).addClass(\"iehover\");\n";
    $my_head .= "	}, function(){\n";
    $my_head .= "	$(this).removeClass(\"iehover\");\n";
    $my_head .= "});\n";
    $my_head .= "});\n";
    $my_head .= "</script>\n";
    $my_head .= "<link rel=\"stylesheet\" type=\"text/css\"	href=\"" . NV_BASE_SITEURL . "themes/" . $module_info['template'] . "/css/category.news.css\" />\n";
    $xtpl->assign("TITLE", $custom_title);
    $xtpl->assign("LINK", $current_link);
    foreach ( $cat as $item )
    {
        $xtpl->assign( 'CAT', $item );
        if ( ! empty( $item['sub'] ) )
        {
            foreach ( $item['sub'] as $sub )
            {
                $xtpl->assign( 'SUB', $sub );
                $xtpl->parse( 'main.item.sub.loop' );
            }
            $xtpl->parse( 'main.item.sub' );
        }
        $xtpl->parse( 'main.item' );
    }
    $xtpl->parse( 'main' );
    $content = $xtpl->text( 'main' );
}

?>